<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <!-- 文件字符编码 -->
    <meta charset="utf-8">
    <!-- 兼容手机,禁止缩放 -->
    <!--		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />-->
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover">
    <!-- 告诉IE使用最新内核 -->
    <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
    <title>排序</title>
    <style type="text/css">

    </style>
</head>
<body>

    <div class="container">
        排序
    </div>
    <script type="text/javascript">
        //请写一个 min 函数，要求 min(numbers) 能返回数组 numbers 中的最小数字。
        let min = (numbers)=>{
            if (numbers.length>2){
                return min([numbers[0],min(numbers.slice(1))]);
            }else {
                return Math.min.apply(null,numbers)
            }
        }
        console.log('数组[2,1,4,65,1,6,8,9]中,最小的数是:'+min([2,1,4,65,1,6,8,9]))



        // 请写出一个 sort 函数，要求 sort(numbers) 能返回一个把 numbers 从小到大排列的数组（你可以添加多余的帮助函数）
        let sort = (numbers)=> {
            if (numbers.length>2){
                let minNumber = min(numbers);
                let minIndex = numbers.indexOf(minNumber);
                numbers.splice(minIndex,1);
                return [minNumber].concat(sort(numbers));
            }else {
                return numbers[0]>numbers[1]?numbers:numbers.reverse();
            }
        }
        console.log('数组[2,1,4,65,1,6,8,9]从小到大排序:');
        console.log(sort([2,1,4,65,1,6,8,9]))


    </script>
</body>
</html>
